﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace SetupExpand
{
    class Program
    {
        static void Main(string[] args)
        {
            //CheckRTXRunning();

            Console.WriteLine("本程序正在检测所需程序...");
            Console.WriteLine();

            //CopyIniFile();
            //Console.WriteLine();

            ChromeSetup();
            Console.WriteLine();

            VCPlugin();
            Console.WriteLine();

            Thread.Sleep(6000);
        }

        private static void CheckRTXRunning()
        {
            bool isNoticeMsg = false;
            while (true)
            {
                Process[] processes = Process.GetProcessesByName("RTX");
                if (processes.Length > 0)
                {
                    if (!isNoticeMsg)
                    {
                        Console.WriteLine("检测到RTX客户端正在运行，请先退出！");
                        isNoticeMsg = true;
                    }
                    Thread.Sleep(1500);
                }
                else
                {
                    break;
                }
            }
        }

        private static void CopyIniFile()
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"advancedGroupClient.ini");
            if (!File.Exists(filePath))
            {
                Console.WriteLine("没有找到配置文件advancedGroupClient.ini!");
                return;
            }

            FileInfo fi = new FileInfo(filePath);
            fi.CopyTo(@"c:\advancedGroupClient.ini", true);
            Console.WriteLine("配置文件advancedGroupClient.ini,已经复制到C盘");
            Thread.Sleep(3000);
        }

        private static void VCPlugin()
        {
            Console.WriteLine();
            string PluginSetupPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"VCPlugin\advancedgroupclient.rpi");
            string PluginbatPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"VCPlugin\setupPlugin.bat");
            Console.WriteLine("RTX高级群插件安装程序路径：" + PluginSetupPath);
            Console.WriteLine();
            if (File.Exists(PluginSetupPath))
            {
                RunSetupExpand(PluginbatPath);
                Console.WriteLine("启动RTX高级群插件安装程序");
            }
            else
            {
                Console.WriteLine("未找到RTX高级群插件安装程序,请手动安装");
                Thread.Sleep(3000);
            }
        }

        private static void ChromeSetup()
        {
            if (!IsInstallChrome())
            {
                Console.WriteLine("未检测到Chrome浏览器程序，开始安装...");
                Console.WriteLine();
                string ChromeSetupPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ChromeSetup\ChromeSetup.exe");
                Console.WriteLine("Chrome浏览器安装程序路径：" + ChromeSetupPath);
                Console.WriteLine();
                if (File.Exists(ChromeSetupPath))
                {
                    RunSetupExpand(ChromeSetupPath);
                    Console.WriteLine("Chrome浏览器安装完成");
                }
                else
                {
                    Console.WriteLine("未找到Chrome安装程序,请手动安装");
                    Thread.Sleep(3000);
                }
            }
            else
            {
                Console.WriteLine("检测到Chrome浏览器已经安装。");
            }
        }

        private static void RunSetupExpand(string setupPath)
        {
            if (File.Exists(setupPath))
            {
                Process myProcess = new Process();
                ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(setupPath);
                myProcessStartInfo.UseShellExecute = false;
                myProcessStartInfo.CreateNoWindow = false;
                myProcessStartInfo.RedirectStandardOutput = true;
                myProcess.StartInfo = myProcessStartInfo;
                myProcess.Start();
                myProcess.WaitForExit();
                myProcess.Close();
            }
        }

        #region 检测Chrome是否安装
        ///<summary> 
        /// 检测是否安装Chrome 
        ///</summary> 
        ///<param name="Chrome_Version"> 获得并返回安装的Chrome版本</param> 
        ///<returns></returns> 
        public static bool IsInstallChrome()
        {
            bool result = false;
            string str_ChromePath = string.Empty;
            string str_ChromeVersion = string.Empty;
            //Chrome_Version = string.Empty;
            //Chrome_Path = string.Empty;

            GetChromePath(out str_ChromePath, out str_ChromeVersion);
            if (!string.IsNullOrEmpty(str_ChromePath) && !string.IsNullOrEmpty(str_ChromeVersion))
            {
                result = true;
                //Chrome_Version = str_ChromeVersion;
                //Chrome_Path = str_ChromePath;
            }
            return result;
        }

        ///<summary> 
        /// 获取并返回当前安装的Chrome版本和安装路径 
        ///</summary> 
        ///<param name="str_ChromePath">Chrome的安装路径</param> 
        ///<param name="str_ChromeVersion">Chrome的安装版本</param> 
        private static void GetChromePath(out string str_ChromePath, out string str_ChromeVersion)
        {
            string str_InstallLocationResult = string.Empty;
            string str_VersionResult = string.Empty;
            string str_InstallLocation = "InstallLocation";
            string str_VersionID = "Version";
            object objResult = null;
            object objInstallLocation = null;
            Microsoft.Win32.RegistryValueKind regValueKind;//指定在注册表中存储值时所用的数据类型，或标识注册表中某个值的数据类型。
            Microsoft.Win32.RegistryKey regKey = null;//表示 Windows 注册表中的项级节点(注册表对象?) 
            Microsoft.Win32.RegistryKey regSubKey = null;
            try
            {
                regKey = Microsoft.Win32.Registry.LocalMachine;//读取HKEY_LOCAL_MACHINE项 
                regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome", false);//如果bool值为true则对打开的项进行读写操作,否则为只读打开

                objInstallLocation = regSubKey.GetValue(str_InstallLocation);
                regValueKind = regSubKey.GetValueKind(str_InstallLocation);
                if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
                {
                    str_InstallLocationResult = objInstallLocation.ToString();
                }

                objResult = regSubKey.GetValue(str_VersionID);
                regValueKind = regSubKey.GetValueKind(str_VersionID);
                if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
                {
                    str_VersionResult = objResult.ToString();
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (regKey != null)
                {
                    regKey.Close();
                    regKey = null;
                }

                if (regSubKey != null)
                {
                    regSubKey.Close();
                    regSubKey = null;
                }
            }
            str_ChromePath = str_InstallLocationResult;
            str_ChromeVersion = str_VersionResult;
        }
        #endregion
    }
}
